/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 *
 */

import fs from 'fs';

import zip from '@zip.js/zip.js';

export class ZipUtil {
  private readonly BEGIN_TIME: number = 1546272000000;

  constructor() {}

  /**
   * 读取zip包根目录的文件信息
   *
   * @param zipPath zip包路径
   * @param fileList  需要读取的文件列表
   */
  static async readFileInZIP(zipPath: string, fileList: string[]) {
    const map = new Map<string, string>();

    const file = fs.readFileSync(zipPath);
    const fileBlob = new Blob([file]);
    const reader = new zip.ZipReader(new zip.BlobReader(fileBlob));
    const entries = await reader.getEntries();
    for (const entry of entries) {
      if (!entry.directory && fileList.includes(entry.filename)) {
        const text = entry.getData && (await entry.getData(new zip.TextWriter()));
        map.set(entry.filename, text ? text : '');
      }
    }
    await reader.close();
    return map;
  }
}
